//登录注册模块
import { reqGetCode,reqUserRegister,reqUserLogin } from "@/api"

//state：仓库存储数据的地方
const state = {
       code:"",
       token:''
    }
//mutation:修改state的唯一手段
const mutations = {
    GETCODE(state,code){
          state.code = code
      },
    USERLOGIN(state,token){
        state.token = token
    }
    }
    //action:书写业务逻辑，处理异步
const actions = {
    // 验证码
      async getCode({commit},phone){
        let re = await reqGetCode(phone)
        if (re.code == 200){
            commit('GETCODE',re.data)
            return 'ok'
        } else {
            return Promise.reject(new Error('faile'))
        }
       },
    //注册
      async userRegister({commit},user){
          let re = await reqUserRegister(user)
            // console.log(re);
            if (re.code == 200){
                return 'ok'
            } else {
                return Promise.reject(new Error('faile'))
            }
       },
    //登录ogin
    async userLogin({commit},data){
        let re = await reqUserLogin(data)
          console.log(re);
          if (re.code == 200){
              commit('USERLOGIN',re.data.token)
              return 'ok'
          } else {
              return Promise.reject(new Error('faile'))
          }
     },
    }
    //getters 简化仓库数据，组件获取仓库数据更加方便
const getters = {}
export default {
    state,
    mutations,
    actions,
    getters
}